(function($) {
    "use strict";

    $.fn.wizardform = function () {
        return this.each(function(){
            var ts=$(this);
            var next_btn=ts.find(".next");
            var prev_btn=ts.find(".previous");
            ts.find('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
                var o=e.target;
                var ot=e.relatedTarget;
                var p=$(o).parent();
                var pp= p.parent();
                var prev_btn=ts.find(".previous");
                var next_btn=ts.find(".next");
                if(p.index()==0){
                    prev_btn.removeClass("jp-primary");
                }
                var li_len=pp.children("li").length;

                if(p.hasClass("disabled")){
                    e.preventDefault();
                    return false;
                }else{
                    if(p.index()==li_len-1){
                        next_btn.html("Finish");
                    }else{
                        next_btn.html("Next");
                    }
                    if(p.index()>0){
                        if(!prev_btn.hasClass("jp-primary")){
                            prev_btn.addClass("jp-primary");
                        }
                    }
                }
            });
            next_btn.click(function(){
                var active=ts.find('ul[role="tablist"] li.active');
                var curr=active.index();
                var next=active.next("li");
                var id=next.children("a").attr("href");
                if(next.hasClass("disabled")){
                    next.removeClass("disabled");
                }
                if(!prev_btn.hasClass("jp-primary")){
                    prev_btn.addClass("jp-primary");
                }
                next.children("a").click();
            });
            prev_btn.click(function(){
                var active=ts.find('ul[role="tablist"] li.active');
                var curr=active.index();
                var prev=active.prev("li");
                prev.children("a").click();
                if(prev.index()==0){
                    prev_btn.removeClass("jp-primary");
                }
            });
        });
    }
}(jQuery));
